package com.hqyj.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.springboot.entity.Users;
import com.hqyj.springboot.mapper.UsersMapper;
import com.hqyj.springboot.repository.UsersRepository;
import com.hqyj.springboot.service.UsersService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private UsersRepository usersRepository;

    //凡是DML语句都应该使用声明式事务观
    //insert/update/delete
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertUser(Users u) throws Exception {
        try {
            usersMapper.insert(u);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertBatchUsers(List<Users> usersList) throws Exception {
        try {
            for (Users u : usersList) {
                usersMapper.insert(u);
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            //出现了异常要回滚....
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        /*
        for (Users u : usersList) {
            usersMapper.insert(u);
        }
        return true;*/
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modifyUsers(Users u) throws Exception {
        try {
            usersMapper.updateById(u);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public Users queryUserByUid(Integer uid) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        try {
            queryWrapper.eq("uid", uid);
            Users u = usersMapper.selectOne(queryWrapper);
            return u;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return null;
        }
    }

    @Override
    public List<Users> queryAllUsers() {
        return usersMapper.selectList(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeUser(Integer uid) throws Exception {
        try {
            usersMapper.deleteById(uid);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    ////////////////////////////////////////////////////////////////////////


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUser(Users u) throws Exception {
        try {
            usersRepository.save(u);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveBatchUsers(List<Users> usersList) throws Exception {
        try {

            usersRepository.saveAll(usersList);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public Users findUserByUid(Integer uid) {
        return usersRepository.getById(uid);
    }

    @Override
    public List<Users> findAllUsers() {
        return usersRepository.findAll();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateUser(Users u) throws Exception {
        try {
            usersRepository.save(u);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteUser(Integer uid) throws Exception {
        try {
            usersRepository.deleteById(uid);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }
}
